package com.zhuidian.mothernote.global;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.base.fragment.AbstractBaseFragment;
import com.base.presenter.BasePresenter;
import com.base.strategy.PopStrategy;
import com.base.util.PopUtil;
import com.zhuidian.mothernote.R;


/**
 * Created by 李响
 * 创建日期 2017/1/21
 * 描述：
 */
public abstract class BaseFragment<T extends BasePresenter> extends AbstractBaseFragment {

    protected BaseActivity baseActivity;
    private RelativeLayout base_rl_top;//所有Activity或Fragment的通用头部
    private ImageView base_iv_back;//头部左边返回键
    private TextView base_tv_title;//头部中间标题
    private LinearLayout base_ll_left;
    private LinearLayout base_ll_middle;
    private LinearLayout base_ll_right;
    private FrameLayout fl_content;//中间内容，由子类实现
    protected T mPresenter;
    private View view;
    private View inflate;
    private View emptyView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        baseActivity = (BaseActivity) getActivity();
        view = inflater.inflate(inflateBaseLayoutId(), container, false);
        return view;
    }

    @Override
    protected int inflateBaseLayoutId() {
        return R.layout.fragment_base;
    }

    @Override
    protected void initBaseView(View view) {
        base_rl_top = (RelativeLayout) view.findViewById(R.id.base_rl_top);
        base_rl_top.setVisibility(View.GONE);
        base_iv_back = (ImageView) view.findViewById(R.id.base_iv_back);
        base_iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishFragment();
            }
        });
        base_tv_title = (TextView) view.findViewById(R.id.base_tv_title);
        base_ll_left = (LinearLayout) view.findViewById(R.id.base_ll_left);
        base_ll_right = (LinearLayout) view.findViewById(R.id.base_ll_right);
        base_ll_middle = (LinearLayout) view.findViewById(R.id.base_ll_middle);
        fl_content = (FrameLayout) view.findViewById(R.id.fl_content);
        inflate = LayoutInflater.from(context).inflate(inflateMainLayoutId(), null);
        fl_content.addView(inflate);
        emptyView = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
        TextView tv_empty = (TextView) (emptyView.findViewById(R.id.tv_empty));
        tv_empty.setText(R.string.network_error);
        fl_content.addView(emptyView);
        emptyView.setVisibility(View.GONE);
    }


    /**
     * 公共布局头部，默认不可见，设置可见
     */
    protected void setTopVisible() {
        base_rl_top.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏头部左边返回键
     */
    protected void setTopLeftGone() {
        base_iv_back.setVisibility(View.GONE);
    }


    public void setTopTitle(String topTitle) {
        base_tv_title.setText(topTitle);
    }


    public void setTopTitle(@StringRes int topTitleRes) {
        base_tv_title.setText(topTitleRes);
    }

    @Override
    public void showProgress() {
        PopStrategy.showProgressDialog(context);
    }

    @Override
    public void hideProgress() {
        PopStrategy.closeProgressDialog();
    }

    @Override
    public void showMsg(String message) {
        PopUtil.toast(context, message);
    }

    @Override
    public void onError() {
        if (emptyView.getVisibility() == View.GONE) {
            emptyView.setVisibility(View.VISIBLE);
            inflate.setVisibility(View.GONE);
        }
    }

    /**
     * 设置顶部左边里面的控件,如果有多个控件，最好是inflate整个多控件的布局进去，当成一个整体
     *
     * @param view
     */
    protected void setTopLeftView(View view) {
        base_ll_left.removeAllViews();
        base_ll_left.addView(view);
    }

    /**
     * 设置顶部中间里面的控件,如果有多个控件，最好是inflate整个多控件的布局进去，当成一个整体
     *
     * @param view
     */
    protected void setTopMiddleView(View view) {
        base_ll_middle.removeAllViews();
        base_ll_middle.addView(view);
    }

    /**
     * 设置顶部右边里面的控件,如果有多个控件，最好是inflate整个多控件的布局进去，当成一个整体
     *
     * @param view
     */
    protected void setTopRightView(View view) {
        base_ll_right.removeAllViews();
        base_ll_right.addView(view);
    }

    protected void setTopLeftImageViewId(@DrawableRes int resId, View.OnClickListener onClickListener) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(resId);
        imageView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        imageView.setPadding(fifteenDp, tenDp, tenDp, tenDp);
        setTopLeftView(imageView);
    }

    protected void setTopRightImageViewId(@DrawableRes int resId, View.OnClickListener onClickListener) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(resId);
        imageView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        imageView.setPadding(tenDp, tenDp, fifteenDp, tenDp);
        setTopRightView(imageView);
    }

    protected void setTopLeftText(@StringRes int resid, View.OnClickListener onClickListener) {
        TextView textView = new TextView(context);
        textView.setText(resid);
        textView.setTextColor(getResources().getColor(R.color.c3));
        textView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        textView.setPadding(fifteenDp, tenDp, tenDp, tenDp);
        setTopRightView(textView);
    }

    protected void setTopRightText(@StringRes int resid, View.OnClickListener onClickListener) {
        TextView textView = new TextView(context);
        textView.setText(resid);
        textView.setTextColor(getResources().getColor(R.color.c5));
        textView.setOnClickListener(onClickListener);
        int tenDp = getResources().getDimensionPixelOffset(R.dimen.ten_dp);
        int fifteenDp = getResources().getDimensionPixelOffset(R.dimen.fifteen_dp);
        textView.setPadding(tenDp, tenDp, fifteenDp, tenDp);
        setTopRightView(textView);
    }

    public MyApplication getMyApplication() {
        return (MyApplication) baseActivity.getApplication();
    }

//    /**
//     * 判断是否已经登录
//     */
//    protected boolean isLoggedIn() {
//        if (getMyApplication().getUser() != null) {
//            return true;
//        }
//        return false;
//    }

//    public void setPromptVisible() {
//        tv_prompt.setVisibility(View.VISIBLE);
//    }
//
//    public void setPromptGone() {
//        tv_prompt.setVisibility(View.GONE);
//    }

    /**
     * 打开一个Fragment
     *
     * @param fragment
     */
    public void startFragment(Fragment fragment) {
        abstractBaseActivity.fragmentStackManager.addFragment(R.id.fl_fragment, fragment);
    }

    /**
     * 结束一个Fragment
     */
    public void finishFragment() {
        abstractBaseActivity.fragmentStackManager.popFragment();
    }

    protected boolean isLogin() {
        return getMyApplication().isLoggedIn();
    }

//    protected LoginBean.DataBean getUser() {
//        return getMyApplication().getUserBean();
//    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
    }
}
